package utils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.github.benmanes.caffeine.cache.Cache;
import com.my.mycommonredis.service.RedisService;

import java.util.concurrent.TimeUnit;

public class CacheUtil {
    public static <T> T getCache(String key, Cache<String,Object> cache,
                                 TypeReference<T> typeReference, RedisService redisService){
        T res=(T)cache.getIfPresent(key);
        if(res!=null){
            return res;
        }
        res = redisService.getCacheObject(key, typeReference);
        if(res!=null){
            cache.put(key,res);
            return res;
        }
        return null;
    }
    public static <T> void setCache(String key, Cache<String,Object> cache,T valeue,
                                 RedisService redisService, Long timeout, TimeUnit unit){
        cache.put(key,valeue);
        redisService.setCacheObject(key,valeue,timeout,unit);
    }
}
